package com.server.sercurity.config;

import com.server.common.utils.JwtTokenUtil;
import com.server.sercurity.filter.TokenAuthenticationFilter;
import com.server.sercurity.handler.CustomAccessDeniedHandler;
import com.server.sercurity.handler.CustomAuthenticationEntryPoint;
import com.server.sercurity.impl.UserDetailsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author zgr
 * @date 2023/7/19 17:00
 * @description
 */
@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtTokenUtil jwtTokenUtil() {
        return new JwtTokenUtil();
    }

    @Bean
    public CustomAccessDeniedHandler customAccessDeniedHandler() {
        return new CustomAccessDeniedHandler();
    }

    @Bean
    public CustomAuthenticationEntryPoint customAuthenticationEntryPoint() {
        return new CustomAuthenticationEntryPoint();
    }

    @Bean
    public TokenAuthenticationFilter tokenAuthenticationFilter(){
        return new TokenAuthenticationFilter();
    }

//    @Bean
//    public AuthenticationManager authenticationManager() {
//        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
//        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
//        return new ProviderManager(daoAuthenticationProvider);
//    }

    @Bean
    public UserDetailsService userDetailsService() {
        return adminName -> userDetailsService.loadUserByUsername(adminName);
    }

//    @Bean
//    public WebSecurityCustomizer webSecurityCustomizer() {
//        return (web) -> web.ignoring().antMatchers(
//                "/home/hi",
//                "/home/index",
//                "/api/**",
//                "/swagger-ui/**",
//                "/generator",
//                "/druid/**",
//                "/swagger-resources/**"
//                ,"/webjars/**"
//                ,"/v3/**"
//                ,"/swagger-ui.html/**"
//        );
//    }

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http.csrf().disable()
                        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                        .and()
                                .authorizeRequests()
                                        .antMatchers("/admin/login", "/home/**", "/generator").permitAll()
                                        .antMatchers("/druid/**").anonymous()
                                        .antMatchers("/v3/**", "/swagger-ui/**", "/swagger-resources/**",
                                                "/webjars/**", "/swagger-ui.html/**").anonymous()
                        .anyRequest().authenticated()
                        .and()
                                .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
//                                    .addFilter(tokenAuthenticationFilter())
                                        .exceptionHandling()
                                                .accessDeniedHandler(customAccessDeniedHandler())
                                                        .authenticationEntryPoint(customAuthenticationEntryPoint())
                                                                .and().cors();
        return http.build();
    }
}
